package ooday04;
//多态的演示
public class PolymorphicDemo {
    public static void main(String[] args) {
        //演示向上造型
        /*
        Dog o1 = new Dog("小黑", 2, "黑色");
        o1.swim();
        o1.eat();o1.lookHome();o1.drink();
        Animal o2 = new Dog("小黄", 2, "换色");
        o2.drink();o2.eat();
        Swim o3 = new Dog("小白", 3, "白色");
        o3.swim();

         */

//演示向上造型（多态）的第一点应用
        /*
        Master master = new Master();
        Dog dog = new Dog("小黑", 2, "黑色");
        Fish fish = new Fish("小鱼", 2, "灰色");
        Chick chick = new Chick("小鸡", 1, "黄色");
        master.feed(dog);//传参的同时系统自动做了向上造型
        master.feed(chick);
        master.feed(fish);

         */

        //演示向上造型（多态）的第二点应用

        Animal[] animals = new Animal[3];
        animals[0] = new Dog("小黑", 2, "黑色");
        animals[1] = new Fish("小鱼", 2, "灰色");
        animals[2] = new Chick("小鸡", 2, "黄色");
        for (Animal animal : animals) {
            System.out.println(animal.name);
            animal.eat();
            animal.drink();

            if (animal instanceof Dog) {
                Dog dog = (Dog) animal;
                dog.lookHome();
            }
            if (animal instanceof Chick) {
                Chick chick = (Chick) animal;
                chick.layEggs();
            }
            //适用于所有实现了Swim接口的（会游泳的）
            if (animal instanceof Swim) {
                Swim s = (Swim) animal;
                s.swim();
            }
        }

        //演示强转成功的条件
        /*
        Animal o = new Dog("小黑", 2, "黑");
        Dog g = (Dog) o;
        Swim s = (Swim) o;
        //Fish f = (Fish) o;//ClassCastException
        System.out.println(o instanceof Dog);
        System.out.println(o instanceof Swim);
        System.out.println(o instanceof Fish);
        if (o instanceof Fish) {//false
            Fish f  = (Fish) o;
        }else {
            System.out.println("o不是Fish类型");
        }

         */

    }
}
